package itbour.onetouchshow.activity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import itbour.onetouchshow.App;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.login.SplashActivity;
import itbour.onetouchshow.adapter.base.BaseAdapterHelper;
import itbour.onetouchshow.adapter.base.normal.QuickAdapter;
import itbour.onetouchshow.bean.ServiceAddressBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.constants.DataCreater;
import itbour.onetouchshow.utils.sharedpreference.PreferencesHelper;
import itbour.onetouchshow.view.ActivityUtils;

/**
 * Created by guoh on 2018/6/25.
 * 功能描述：选择服务器地址
 * 需要的参数：
 */
public class ServiceAddressActivity extends Activity {

    private ListView listView;
    private ServiceAddressAdapter serviceAddressAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_service_address);
        findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String address = ((EditText) findViewById(R.id.et_input_address)).getText().toString();
                serviceAddressAdapter.add(new ServiceAddressBean(address, "手动添加"));
            }
        });
        listView = findViewById(R.id.ls_address);
        serviceAddressAdapter = new ServiceAddressAdapter(ServiceAddressActivity.this);
        listView.setAdapter(serviceAddressAdapter);
        serviceAddressAdapter.replaceAll(DataCreater.getServiceAddress());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //更换服务器地址
                PreferencesHelper.saveData(AppConst.KEY_SERVICE_ADDRESS, serviceAddressAdapter.getItem(position).getAddress());
                //清除用户信息
                App.loginOut();


                AlertDialog.Builder alert = new AlertDialog.Builder(ServiceAddressActivity.this);
                alert.setTitle("提示").setMessage("更换了服务器地址需要重启App" + "\n" + "(下次进入为未登录状态)").setPositiveButton("还是重启",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                reStartApp();
                            }
                        }).setNegativeButton("重启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        reStartApp();
                    }
                });
                alert.create();
                alert.show();
            }
        });
    }

    private void reStartApp() {
        Intent intent = new Intent(ServiceAddressActivity.this, SplashActivity.class);
        PendingIntent restartIntent = PendingIntent.getActivity(ServiceAddressActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager) ServiceAddressActivity.this.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 50, restartIntent); // 1秒钟后重启应用

        ActivityUtils.removeAllActivity();
        android.os.Process.killProcess(android.os.Process.myPid());
        finish();
    }

    private class ServiceAddressAdapter extends QuickAdapter<ServiceAddressBean> {

        public ServiceAddressAdapter(Context context) {
            super(context, R.layout.item_service_address);
        }

        @Override
        protected void convert(BaseAdapterHelper helper, ServiceAddressBean item, int position) {
            helper.setText(R.id.tv_address, item.getAddress());
            helper.setText(R.id.tv_name, "( " + item.getName() + " )");
        }
    }
}
